Skip to content

feat(middleware): add failure recovery to security middleware - #1048

Merged
imDarshanGK merged 4 commits into
imDarshanGK:mainfrom
Shriraj888:feat/add-failure-recovery-to-security-middleware
Jul 31, 2026
Merged

feat(middleware): add failure recovery to security middleware #1048
imDarshanGK merged 4 commits into
imDarshanGK:mainfrom
Shriraj888:feat/add-failure-recovery-to-security-middleware

Conversation

@Shriraj888

Copy link
Copy Markdown
Contributor

Description

Resolves #920.

Improves OriginValidationMiddleware (backend/middleware/csrf.py) with fail-closed failure recovery. When origin extraction or validation encounters an unexpected runtime error (e.g. malformed headers or parsing faults), the middleware catches the exception, logs a detailed error traceback, and securely rejects the request with an HTTP 500 JSON response rather than allowing unvalidated requests to proceed or causing unhandled crashes.

Key Changes

  • Security Middleware (backend/middleware/csrf.py):

    • Wrapped origin header extraction and validation logic in dispatch() inside a try...except Exception block.
    • Logged errors with logger.error("Failure recovery triggered in security middleware...", exc_info=True).
    • Returned fail-closed JSONResponse({"detail": "Security verification error: middleware failure recovery triggered"}, status_code=500).
  • Unit Tests (backend/tests/test_csrf.py):

    • Added test_security_middleware_failure_recovery_on_exception to verify that mutating requests return an HTTP 500 response when header extraction/validation raises an exception.
    • Added test_security_middleware_failure_recovery_safe_methods to verify that safe HTTP methods (GET) bypass origin verification and succeed normally.
  • Documentation (docs/csrf-protection.md):

    • Documented failure recovery behavior in the Implementation and Design Decisions sections.

Acceptance Criteria Checklist

  • Implement the change in the security middleware (OriginValidationMiddleware).
  • Add or update unit tests (test_csrf.py).
  • Update documentation (docs/csrf-protection.md).

Verification

Run the test suite:

python -m pytest backend/tests/test_csrf.py -v

@vercel

vercel Bot commented Jul 30, 2026

Copy link
Copy Markdown

@Shriraj888 is attempting to deploy a commit to the Darshan's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Shriraj888

Copy link
Copy Markdown
Contributor Author

@imDarshanGK The PR is ready for review. Looking forward to your feedback. Thanks!

@imDarshanGK imDarshanGK added SSoC26 Part of Social Summer of Code 2026 Hard Larger or more advanced changes labels Jul 31, 2026
@imDarshanGK
imDarshanGK merged commit e30cb1e into imDarshanGK:main Jul 31, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hard Larger or more advanced changes SSoC26 Part of Social Summer of Code 2026

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add failure recovery to security middleware

2 participants